package com.xgame.server.dbtool;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.xgame.server.db.mapper.BagitemMapper;
import com.xgame.server.db.model.Bagitem;
import com.xgame.server.netty.proto.Game;
import org.springframework.stereotype.Component;


import javax.annotation.Resource;
import java.util.List;

@Component
public class BagCsTool {
    @Resource
    private BagitemMapper bagitemMapper;

    //插入背包物品
    public Integer insertBagItem(Game.ItemCs data) throws Exception {
        //1.首先删除该id已经有的物品
        delteBagItemByAccount(data.getAccount());

        //2.插入新的物品表
        Integer result = new Integer(0);

        for (int i = 0; i < data.getArrayList().size(); i++) {
            Game.ItemOneCs one = data.getArray(i);
            Bagitem bag = new Bagitem();
            bag.setAccount(data.getAccount());
            bag.setPlayerId(data.getPlayerID());
            bag.setGuid(one.getGuid());
            bag.setItemId(one.getItemId());
            bag.setNum(one.getNum());
            bag.setEnhanceLv(one.getEnhanceLv());

            result += bagitemMapper.insert(bag);
        }

        return result;
    }

    //删除所有玩家的物品
    public Integer delteBagItemByAccount(String account) throws Exception {
        EntityWrapper<Bagitem> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("account", account);
        return bagitemMapper.delete(entityWrapper);
    }

    //删除某玩家的某一些物品
    public Integer deleteBagItemsByPlayerId(String account, List<Integer> guids) throws Exception {
        EntityWrapper<Bagitem> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("account", account);
        entityWrapper.in("guid", guids);

        return bagitemMapper.delete(entityWrapper);
    }

    //查询某玩家的所有物品
    public List<Bagitem> selectBagItems(String account) throws Exception {
        EntityWrapper<Bagitem> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("account", account);

        return bagitemMapper.selectList(entityWrapper);
    }
}
